package com.dayaokeji.rhythmschoolstudent.client.common;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.common.bean.SignQrCodeContent;
import com.dayaokeji.rhythmschoolstudent.utils.MD5Util;
import com.dayaokeji.rhythmschoolstudent.utils.PrefUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.WifiUtil;
import com.github.sumimakito.awesomeqr.AwesomeQRCode;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 生成二维码
 */
public class GenerateQRCodeActivity extends BaseActivity {

    public static final String EXTRA_COURSE_TITLE = "course_title";
    public static final String EXTRA_SIGN_WIFI = "sign_wifi";

    public static final int EXPIRSE_TIME =  5;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.tv_timer)
    TextView tvTimer;


    private String courseTitle;
    private String signWifi;
    private final Gson gson = new Gson();
    private Disposable subscribe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generate_qrcode);
        setSupportActionBar(toolbar);

        courseTitle = getIntent().getStringExtra(EXTRA_COURSE_TITLE);
        signWifi = getIntent().getStringExtra(EXTRA_SIGN_WIFI);

        init();
    }

    private void init() {

        setTitle(courseTitle);
        generate();
    }

    private void generate() {
        generateQRCode();
        if (ivQrCode != null) {
            ivQrCode.postDelayed(new Runnable() {
                @Override
                public void run() {
                    generateQRCode();
                    if (ivQrCode != null) {
                        ivQrCode.postDelayed(this,EXPIRSE_TIME * 1000L);
                    }
                }
            },EXPIRSE_TIME * 1000L);
        }
        startTimer();
    }



    private void startTimer() {
        if (tvTimer != null) {
            tvTimer.setText(getString(R.string.generate_qr_code_timer,EXPIRSE_TIME));
            tvTimer.setTag(EXPIRSE_TIME);
            tvTimer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (tvTimer != null) {
                        int sec = tvTimer.getTag() == null ? 0 : (int) tvTimer.getTag()-1;
                        if (sec > 0) {
                            tvTimer.setText(getString(R.string.generate_qr_code_timer,sec));
                            tvTimer.setTag(sec);
                            tvTimer.postDelayed(this,1000L);
                        }else{
                            tvTimer.setText(getString(R.string.generate_qr_code_timer,EXPIRSE_TIME));
                            tvTimer.setTag(EXPIRSE_TIME);
                            tvTimer.postDelayed(this,1000L);
                        }
                    }
                }
            },1000L);
        }
    }


    /**
     * 生成二维码
     */
    @SuppressLint("CheckResult")
    private void generateQRCode() {
        if (subscribe != null) {
            subscribe.dispose();
        }
        subscribe = WifiUtil.getWifiList().subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                if (!strings.isEmpty()) {
                    if (WifiUtil.checkMacAddress(signWifi, strings)) {
                        if (ivQrCode != null) {
                            final SignQrCodeContent signQrCodeContent = new SignQrCodeContent();
                            signQrCodeContent.setDate((System.currentTimeMillis() + PrefUtils.getNetTimeDifference()) / 1000);
                            signQrCodeContent.setLoc_array(strings);
                            String checkCode = signQrCodeContent.getDate() + "dayaokeji";
                            signQrCodeContent.setCheckcode(MD5Util.md5(checkCode));
                            String qrCodeStr = gson.toJson(signQrCodeContent);
                            try {
                                Bitmap bitmap = AwesomeQRCode.create(qrCodeStr, 700, 10, 0.7F, Color.BLACK, Color.WHITE, null, false, true);
                                ivQrCode.setImageBitmap(bitmap);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        ToastUtils.info("没有扫描到能签到的WIFI列表，无法生成二维码");
                        if (tvTimer != null) {
                            tvTimer.setVisibility(View.GONE);
                        }
                    }
                } else {
                    ToastUtils.warning("没有扫描到WIFI列表，生成二维码无效");
                    if (tvTimer != null) {
                        tvTimer.setVisibility(View.GONE);
                    }
                }
            }
        });
    }
}
